﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WAD.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace WAD.DAL
{
    /// <summary>
    /// 广告点击数据库操作类
    /// </summary>
    public class DaoAdvertClick : DAO
    {
        /// <summary>
        /// 添加一条点击量
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static bool Add(MAdvertClick m)
        {
            String sql = "insert into advert_click(shared_id)values(@shared_id)";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@shared_id", DbType.Int32, m.shared_id);
            return db.ExecuteNonQuery(cmd) > 0;
        }



        /// <summary>
        /// 获取某条广告的点击情况分页
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页显示的总条数</param>
        /// <returns></returns>
        public static PageData GetListByAdvert(int pageIndex, int pageSize, int advertId, int day)
        {
            String fieldList = "id,shared_id,CONVERT(varchar(20), [date], 20) [date],[address],os,browser";
            String orderBy = "id desc";
            String tableName = "v_advert_click";
            String where = "advert_id = " + advertId;

            if (day != 0)
            {
                where += " and dateDiff(dd,CONVERT(varchar(11), GETDATE()-" + day + ", 23)+' 00:00',[date]) > 0";
            }
            return GetPageList(fieldList, orderBy, pageIndex, pageSize, where, tableName);
        }



        /// <summary>
        /// 获取某条分享的广告的点击情况分页
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页显示的总条数</param>
        /// <returns></returns>
        public static PageData GetListByShared(int pageIndex, int pageSize, int sharedId, int day)
        {
            String fieldList = "id,shared_id,CONVERT(varchar(20), [date], 20) [date],[address],os,browser";
            String orderBy = "id";
            String tableName = "v_advert_click";
            String where = "shared_id = " + sharedId;

            if (day != 0)
            {
                where += " and dateDiff(dd,CONVERT(varchar(11), GETDATE()-" + day + ", 23)+' 00:00',[date]) > 0";
            }
            return GetPageList(fieldList, orderBy, pageIndex, pageSize, where, tableName);
        }


        /// <summary>
        /// 获取用户的广告的点击情况分页
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页显示的总条数</param>
        /// <returns></returns>
        public static PageData GetListByUser(int pageIndex, int pageSize, int userId, int day)
        {
            String fieldList = "id,shared_id,CONVERT(varchar(20), [date], 20) [date],[address],os,browser";
            String orderBy = "id";
            String tableName = "v_advert_click";
            String where = "[user_id] = " + userId;

            if (day != 0)
            {
                where += " and dateDiff(dd,CONVERT(varchar(11), GETDATE()-" + day + ", 23)+' 00:00',[date]) > 0";
            }
            return GetPageList(fieldList, orderBy, pageIndex, pageSize, where, tableName);
        }
    }
}
